﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IR
{
    class MatchingSystem
    {
        Index m_Index;
        DocumentsList m_docs; //문서길이를 가져야함.
        int m_Num_Of_Total_Docs;
        string m_Query;
        bool m_Ready;


        public bool Ready
        {
            get { return m_Ready; }
            set { m_Ready = value; }
        }
        public string Query
        {
            get { return m_Query; }
            set { m_Query = value; }
        }

        public MatchingSystem(Index index, int num_of_total_docs)
        {
            m_Index = index;
            m_Num_Of_Total_Docs = num_of_total_docs;    
        }
        

        public void Init()
        {

            DirectoryInfo m_Dinfo;
            string currentDrct = Environment.CurrentDirectory;
            StreamReader fReader;
            char[] chr = { '.', 't', 'x', 't' };

            m_Dinfo = new DirectoryInfo(currentDrct + @"\data");
            m_docs = new DocumentsList();
            

            for (int i = 0; i < m_Num_Of_Total_Docs; i++ )
                m_docs.Add(new Document(i + 1));

            foreach (IR_Dictionary I in m_Index.IR_Dct)
            {

                foreach (Post P in I.Postings)
                {
                   
                    m_docs[P.Doc_number - 1].Docs_Length += P.Wt * P.Wt;
                    
                }
            }
            foreach (Document d in m_docs )
            {
                d.Docs_Length = Math.Sqrt(d.Docs_Length);

            }

           
            foreach (FileInfo f in m_Dinfo.GetFiles())
            {

                fReader = new StreamReader(f.OpenRead(), Encoding.Default);//스트림 열고 
                int docNumber = Convert.ToInt32((f.Name.Trim(chr)));

                string temp = fReader.ReadLine();
                m_docs[docNumber-1].Preview = temp;
                m_docs[docNumber-1].Path = f.FullName;
                fReader.Close();

               
            }
             
            foreach (IR_Dictionary I in m_Index.IR_Dct)
            {
                foreach (Post P in I.Postings)
                {

                    P.Nomalized = P.Wt / m_docs[P.Doc_number - 1].Docs_Length;

                }  
            }




            FileStream fff = new FileStream("debug_ltnlnc.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fff);
            foreach (Document d in m_docs)
            {
                sw.WriteLine("번호 " + d.Doc_number + " " + "우선순위 : " + d.Priority + " 문서길이 : " + d.Docs_Length);

            }
            sw.Close();


            m_Ready = true;
 

        }
        public DocumentsList Retreival(string Query)
        {
            DocumentsList results;
            String[] querys;

            results = (DocumentsList)m_docs.Clone() ;

            foreach (Document d in results)
            {
                d.Priority= 0;

            }
            querys = Query.Split();


            foreach (string q in querys)
            {
                if (q.CompareTo("") == 0)
                    continue;
                foreach (IR_Dictionary I in m_Index.IR_Dct)
                {
                    if (I.Term == q)
                    {
                        foreach (Post P in I.Postings)
                        {
                            results[P.Doc_number - 1].Priority += P.Nomalized;


                        }
                    }

                }
            }

            
            results.Sort();
            results.Reverse();

            return results;//정열

        }

       
    }
}
